package com.quanyan.stadium.biz.redis.impl;

import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.redisCluster.utils.SerializeUtil;
import com.quanyan.stadium.biz.redis.UserCategoryScoreCacheDao;
import com.quanyan.stadium.component.cache.MyRedisTemplate;
import com.quanyan.stadium.component.cache.StadiumRedisConstants;
import com.quanyan.stadium.entity.TbUserScoreCollect;
import com.quanyan.stadium.utils.StadiumDateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Tuple;

import java.util.*;

/**
 * @author xiaoxiao
 * @date 16/4/11-14:42
 *
 * 缓存基础类
 */

@Service("UserCategoryScoreCacheDaoImpl")
public class UserCategoryScoreCacheDaoImpl implements UserCategoryScoreCacheDao {

    private final static Logger logger = LoggerFactory.getLogger(UserCategoryScoreCacheDaoImpl.class);

    @Autowired
    private MyRedisTemplate myRedisTemplate;


    @Override
    public void initCategoryScoreRankMsgToCache(Integer cityId, Integer categoryId, List<TbUserScoreCollect> list, Boolean isFirst) {
        String key = "";
        if (null == cityId || cityId.intValue() == 0) {
            key = StadiumRedisConstants.getCategoryCountryPkScoreKEY(categoryId);
        }else {
            key = StadiumRedisConstants.getCategoryCityPkScoreKEY(categoryId, cityId);
        }
        Map<byte[], Double> scoreMembers = new HashMap<>();
        for (TbUserScoreCollect ca : list) {
            scoreMembers.put(SerializeUtil.serialize(ca), Double.valueOf(ca.getScore()));
        }
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        myRedisTemplate.zadd(redisKey, scoreMembers);

    }


    @Override
    public List<TbUserScoreCollect> queryCategoryScoreRankMsgFromCache(Integer cityId, Integer categoryId, Integer begin, Integer end) {
        List<TbUserScoreCollect> result = new ArrayList<>();
        String key = "";
        if (null == cityId || cityId.intValue() == 0) {
            key = StadiumRedisConstants.getCategoryCountryPkScoreKEY(categoryId);
        }else {
            key = StadiumRedisConstants.getCategoryCityPkScoreKEY(categoryId, cityId);
        }
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        Set<Tuple> msg =  myRedisTemplate.zrangeWithScores(redisKey, begin, end);
        if (null == msg || msg.size() == 0) {
            return result;
        }
        for (Tuple tuple : msg) {
            result.add((TbUserScoreCollect) SerializeUtil.unserialize(tuple.getBinaryElement()));
        }
        return result;
    }

    @Override
    public void initCategoryScoreRankNumToCache(Integer cityId, Integer categoryId, Integer num) {
        String key = "";
        if (null == cityId || cityId.intValue() == 0) {
            key = StadiumRedisConstants.getCategoryCountryPkScoreTotalKEY(categoryId);
        }else {
            key = StadiumRedisConstants.getCategoryCityPkScoreTotalKEY(categoryId, cityId);
        }
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        myRedisTemplate.setWithExpireTime(redisKey, num, this.getNowToNightSec());
    }

    @Override
    public int getCategoryScoreRankNumFromCache(Integer cityId, Integer categoryId) {
        String key = "";
        if (null == cityId || cityId.intValue() == 0) {
            key = StadiumRedisConstants.getCategoryCountryPkScoreTotalKEY(categoryId);
        }else {
            key = StadiumRedisConstants.getCategoryCityPkScoreTotalKEY(categoryId, cityId);
        }
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        Object o = myRedisTemplate.get(redisKey);
        if (null!=o) {
            return (int) o;
        }
        return 0;
    }

    @Override
    public int getCategoryScoreRankStoreNumFromCache(Integer cityId, Integer categoryId) {
        String key = "";

        if (null == cityId || cityId.intValue() == 0) {
            key = StadiumRedisConstants.getCategoryCountryPkScoreKEY(categoryId);
        }else {
            key = StadiumRedisConstants.getCategoryCityPkScoreKEY(categoryId, cityId);
        }
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        return myRedisTemplate.zcard(redisKey).intValue();
    }

    @Override
    public void initUserRankNum(Integer cityId, Integer categoryId, Integer uid, Integer rank) {
        String key = "";
        if (null == cityId || cityId.intValue() == 0) {
            key = StadiumRedisConstants.getUserCategoryScoreRankNum(categoryId, uid);
        }else {
            key = StadiumRedisConstants.getUserCategoryAndCityScoreRankNum(categoryId, cityId, uid);
        }
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        myRedisTemplate.setWithExpireTime(redisKey, rank, this.getNowToNightSec());
    }

    @Override
    public Integer getUserRankNum(Integer cityId, Integer categoryId, Integer uid) {
        String key = "";
        if (null == cityId || cityId.intValue() == 0) {
            key = StadiumRedisConstants.getUserCategoryScoreRankNum(categoryId, uid);
        }else {
            key = StadiumRedisConstants.getUserCategoryAndCityScoreRankNum(categoryId, cityId, uid);
        }
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        return (Integer)myRedisTemplate.get(redisKey);
    }

    /**
     * 获取当前时间到凌晨相差多少秒
     *
     * */
    private int getNowToNightSec(){
        return (int) ((StadiumDateUtils.getTimesnight().getTime() - new Date().getTime())/1000);
    }

}
